﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoServey
{
    class frmMainMethods
    {
        FileOperator fileOperator = new FileOperator();
        public string StartCatchSv(string zipPath)
        {
            string filePath = string.Format(@"{0}/CatchingSVTask", FrmMain.GetInstance().AppPath);
            ProcessMethods processMethods = new ProcessMethods();
            processMethods.unzipFile(filePath, zipPath);
            Thread.Sleep(2000);
            selectTask(filePath);
             processMethods = new ProcessMethods();
            string resultPath = string.Format(@"{0}/CatchedSVTask/{1}.zip", 
                FrmMain.GetInstance().AppPath,Guid.NewGuid().ToString());
            processMethods.zipFile(string.Format(@"{0}/CatchedSVTask", FrmMain.GetInstance().AppPath), resultPath);
            return resultPath;
        }

        public void selectTask(string filesPath)
        {
            List<string> files = new List<string>();
            fileOperator.getAllFiles(filesPath, files);

            while (files.Count > 0)
            {
                foreach (string path in files)
                {
                    dealEachTask(path);
                }
                files = new List<string>();
                fileOperator.getAllFiles(filesPath, files);
            }
            Console.WriteLine("CatchSV Completed!");
        }

        /// <summary>
        /// 处理每一个任务
        /// </summary>
        /// <param name="path"></param>
        public void dealEachTask(string path)
        {
            XmlInput xmlInput = new XmlInput();
            if (xmlInput.getTaskType(path) == "STREET_PARKING_METER_SIGN_COLLECTION")
            {
                FrmMain.GetInstance().xmlPath = path;
                if (xmlInput.hasPanoLocation(path) == true)
                {
                    string streetViewPath = Path.GetDirectoryName(path) + @"/StreetView";
                    if (Directory.Exists(streetViewPath))
                    {
                        fileOperator.MoveDirectory(Path.GetDirectoryName(path), FrmMain.GetInstance().AppPath + "/CatchedSVTask");
                        return;
                    }
                    xmlInput.readXml(path);
                }
                else
                {
                    string geometry = xmlInput.getGeometry(path);
                    GooglePanoSearch.geometry = geometry;
                    GooglePanoSearch.GetInstance().ShowDialog();
                }
            }
            else
            {
                Console.WriteLine("不能加载该类型任务！");
            }
        }
      
        /// <summary>
        /// 求直线斜率
        /// </summary>
        /// <param name="latlngs"></param>
        /// <returns></returns>
        public double[] getK(string[] latlngs)
        {
            double x1 = Convert.ToDouble(latlngs[0].Trim().Split(' ')[0]);
            double y1 = Convert.ToDouble(latlngs[0].Trim().Split(' ')[1]);

            double x2 = Convert.ToDouble(latlngs[1].Trim().Split(' ')[0]);
            double y2 = Convert.ToDouble(latlngs[1].Trim().Split(' ')[1]);

            double k = (y2 - y1) / (x2 - x1);
            double b = y1 - x1 * k;
            return new double[] { k, b };
        }

        /// <summary>
        /// 求采集朝向
        /// </summary>
        /// <param name="geometry"></param>
        /// <param name="panoLocation"></param>
        public double getOritation(string geometry, string panoLocation)
          {
              string geo = geometry.Split('(')[1].Split(')')[0];
              string[] geoLatlngs = geo.Split(',');
              double[] line = getK(geoLatlngs);
              panoLocation = panoLocation.Replace("),(", ";");
              string[] latlngs = panoLocation.Split(';');

              int left = 0;

              for (int i = 0; i < latlngs.Length; i++)
              {
                  string latlng = latlngs[i];
                  if (i == 0)
                  {
                      latlng = latlng.Split('(')[1];
                  }
                  else if (i == latlngs.Length - 1)
                  {
                      latlng = latlng.Split(')')[0];
                  }
                  double lat = Convert.ToDouble(latlng.Split(',')[0].Trim());
                  double lng = Convert.ToDouble(latlng.Split(',')[1].Trim());

                  if ((lng * line[0] + line[1]) < lat)
                  {
                      left++;
                  }
                  else
                  {
                      left--;
                  }
              }
              double angle = rad(Math.Atan(line[0]));
              double orientation=modifyOrientation(left,angle);
              return orientation;
          }

        /// <summary>
        /// 根据任务线斜率及街景点与任务线位置计算采集朝向
        /// </summary>
        /// <param name="left"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public double modifyOrientation(int left, double angle)
        {
            double orientation;
            if (left > 0)
            {
                orientation = angle - 90;
            }
            else
            {
                orientation = angle + 90;
            }
            if (orientation > 180)
            {
                orientation -= 180;
            }
            else if (orientation < -180)
            {
                orientation += 180;
            }
            orientation = 90 - orientation;
            if (orientation < 0)
            {
                orientation += 360;
            }
            else if (orientation > 360)
            {
                orientation -= 360;
            }
            return orientation;
        }

        /// <summary>
        /// 弧度转角度
        /// </summary>
        /// <param name="angle"></param>
        /// <returns></returns>
        public double rad(double angle)
        {
            return angle * 180 / Math.PI;
        }

        public void deleteAllCache()
        {
            string NewCatchSVPath = string.Format(@"{0}/NewCatchSVTask", FrmMain.GetInstance().AppPath);
            try
            {
            	Directory.Delete(NewCatchSVPath, true);
            }
            catch (System.Exception ex)
            {
            	
            }
            Directory.CreateDirectory(NewCatchSVPath);
            string CatchingSVPath = string.Format(@"{0}/CatchingSVTask", FrmMain.GetInstance().AppPath);
            try
            {
            	Directory.Delete(CatchingSVPath, true);
            }
            catch (System.Exception ex)
            {
            	
            }
            Directory.CreateDirectory(CatchingSVPath);
        }
       
    }
}
